package src.com.servlet;

import src.com.data.User;
import src.com.service.MyService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class LogLet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        MyService service  = new MyService();
        User user = new User();
        user.setName(userName);
        user.setPassword(password);
        int flag = service.login(user);
        int user_id = service.getUserId(user);
        if(flag!=0){
            System.out.println("登录成功");
            System.out.println(user_id);
        }else{
            System.out.println("登录失败");
        }

        //返回信息到客户端
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        if(flag!=0){
            // 登录成功给app返回用户id
            out.print("用户id："+user_id);
        }else{
            out.print("登录失败");
        }
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
